# coding: UTF-8

SPACE = 0
MARU = 1
BATSU = -1
MARKS = (" ", "O", "X")

class Cell(object):
    def __init__(self):
        self.value = SPACE
    
    def set(self, value):
        self.value = value
    
    def getmark(self):
        return MARKS[self.value]
    
    def is_empty(self):
        return self.value == SPACE

LINES = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
VALUES = (MARU, BATSU)

class Board(object):
    def __init__(self):
        self.cells = [Cell() for i in range(9)]
    
    def display(self):
        print("+---+---+---+")
        cell_line = "|"
        for i in range(9):
            cell_line += " " + self.cells[i].getmark() + " |"
            if i % 3 == 2:
                print(cell_line)
                cell_line = "|"
                print("+---+---+---+")
    
    def is_empty(self, index):
        return self.cells[index].is_empty()
    
    def __count(self):
        count = 0
        for current in self.cells:
            if not current.is_empty():
                count += 1
        return count
    
    def winner(self):
        return MARKS[VALUES[(self.__count() - 1) % 2]]
    
    def put(self, index):
        self.cells[index].set(VALUES[self.__count() % 2])
    
    def is_lined(self):
        for current in LINES:
            total = 0
            for i in current:
                total += self.cells[i].value
                if abs(total) == 3:
                    return True
        return False

    def __is_filled(self):
        return self.__count() > 8
    
    def is_finished(self):
        return self.__is_filled() or self.is_lined()

if __name__ == '__main__':
    board = Board()
    while not board.is_finished():
        row = input("Rows? : ")
        if row not in(1,2,3):
            print("Input 1, 2 or 3.")
            continue
        
        column = input("Column? : ")
        if column not in(1,2,3):
            print("Input 1, 2 or 3.")
            continue
        
        target_index = (row - 1) * 3 + (column - 1)
        if board.is_empty(target_index):
            board.put(target_index)
        else:
            print("Not empty. Try another.")
            continue
        board.display()
    
    if board.is_lined():
        print("Game over. " + board.winner() + " won.")
    else:
        print("Game over. Draw.")
